
/******************************************************************/
/*                    DoFile Replication file
	Review of Economics and Statistics
	
		Victim incentives and criminal activity: 
		Evidence from bus driver robberies in Chile   		  
			
			Author: Patricio Domínguez
			September, 2020										  */

/******************************************************************/

/* Notes: This dofile generates Table 4*/

set scheme plotplain

/******************************************************************/
/*                    Set directory structure          		      */ 
/******************************************************************/


global rep "/Users/patriciodominguez/RES_Dominguez Dropbox/Patricio Dominguez/Replication/TS"

use "$rep/dta/panel_muni_week.dta", clear
drop if comuna_id==4|comuna_id==5|comuna_id==31

g pre =0
replace pre=1 if week<42
 
g tra =0
replace tra=1 if week>=42 & week<110
 
g post =0
replace post=1 if week>=110

g period =0 if pre
replace period =1 if tra
replace period=2 if post
g pre_cash = pre*cash
g post_cash = post*cash
g tra_cash = tra*cash

g bus_tra = bus*tra
g bus_post = bus*post
g bus_cash =bus*cash

g pre_cash_bus = pre_cash*bus
g post_cash_bus = post_cash*bus
g tra_cash_bus = tra_cash*bus

global TS tra post
global DD $TS cash tra_cash post_cash
global DDD $DD bus bus_tra bus_post bus_cash tra_cash_bus post_cash_bus

g crime_T=.
levelsof comuna_id, local(levels)
foreach l of local levels{
	forvalues j=0/1 {
		forvalues k=0/1 {
			quietly su crime if !tra&!post&bus==`j'&cash==`k'&comuna_id ==`l' 
			replace crime_T=crime/`r(mean)' if bus==`j'&cash==`k'&comuna_id ==`l' 
		}
}
}

g year=2005 if week<52
replace year=2006 if week>=52 &week<104
replace year=2007 if week>=104 &week<156
replace year=2008 if week>=156 &week<208
replace year=2009 if week>=208 &week<260
replace year=2010 if week>=260 
g month = int((week)/52*12)+1 if year ==2005
replace month = int((week-52+1)/52*12)+1 if year ==2006
replace month = int((week-52*2)/52*12)+1 if year ==2007
replace month = int((week-52*3)/52*12)+1 if year ==2008
replace month = int((week-52*4)/52*12)+1 if year ==2009
replace month = int((week-52*5)/52*12)+1 if year ==2010
replace month=12 if month==13

bysort comuna_id period: egen aux=mean(crime) if period==0 & bus 
bysort comuna_id: egen weight=mean(aux) 
su weight crime

egen long newid = group(comuna_id cash bus) 
egen t = group(cash bus week)

xtset week newid
xtset comuna_id t 
est drop _all
qui reg crime_T i.comuna_id i.month $TS if bus &cash, cl(comuna_id) 
est sto TS1
qui reg crime_T i.comuna_id i.month $TS if bus &cash [pweight = weight], cl(comuna_id) 
est sto TS2
qui reg crime_T i.comuna_id i.month $DD if bus, cl(comuna_id) 
est sto DD_1
qui reg crime_T i.comuna_id i.month $DD if bus [pweight = weight], cl(comuna_id) 
est sto DD_2
qui reg crime_T i.comuna_id i.month $DDD, cl(comuna_id)  
est sto DDD1
qui reg crime_T i.comuna_id i.month $DDD [pweight = weight], cl(comuna_id) 
est sto DDD2		

* Table 4. Panels A, B and C, Columns 1 and 2
esttab TS*, keep (tra post) se r2 
esttab DD_1 DD_2, keep (tra_cash post_cash) se r2 
esttab DDD1 DDD2, keep (tra_cash_bus post_cash_bus) se r2 

*****************************************************************************
************ Collapsing to one observation per comuna x period
*****************************************************************************

collapse (sum) crime, by(cash bus comuna_id period)
replace crime = crime/42 if period ==0
replace crime = crime/(110-42) if period ==1
replace crime = crime/(313-110) if period ==2

g pre =0 if period!=0
replace pre=1 if period==0
 
g tra =0 if period!=1
replace tra=1 if period==1
 
g post =0 if period!=2
replace post=1 if period==2

g pre_cash = pre*cash
g post_cash = post*cash
g tra_cash = tra*cash

g bus_tra = bus*tra
g bus_post = bus*post
g bus_cash =bus*cash

g pre_cash_bus = pre_cash*bus
g post_cash_bus = post_cash*bus
g tra_cash_bus = tra_cash*bus

g crime_T=.
levelsof comuna_id, local(levels) 
foreach l of local levels{
	forvalues j=0/1 {
		forvalues k=0/1 {
			quietly su crime if !tra&!post&bus==`j'&cash==`k'&comuna_id==`l' 
			replace crime_T=crime/`r(mean)' if bus==`j'&cash==`k'&comuna_id==`l' 
		}
}
}

global TS tra post
global DD $TS cash tra_cash post_cash
global DDD $DD bus bus_tra bus_post bus_cash tra_cash_bus post_cash_bus

bysort comuna_id period: egen aux=mean(crime) if period==0 & bus 
bysort comuna_id: egen weight=mean(aux)  
su weight,d

egen long newid = group(comuna_id cash bus) 

est drop _all
xtset period newid
qui reg crime_T i.comuna_id $TS if bus &cash, cl(comuna_id) 
est sto TS3
qui reg crime_T i.comuna_id $TS if bus &cash [pweight = weight], cl(comuna_id) 
est sto TS4
qui reg crime_T i.comuna_id $DD if bus, cl(comuna_id) 
est sto DD_3
qui reg crime_T i.comuna_id $DD if bus [pweight = weight], cl(comuna_id) 
est sto DD_4
qui reg crime_T i.comuna_id $DDD, cl(comuna_id)  
est sto DDD3
qui reg crime_T i.comuna_id $DDD [pweight = weight], cl(comuna_id) 
est sto DDD4		

* Table 4. Panels A, B and C, Columns 3 and 4
esttab TS*, keep (tra post) se r2 
esttab DD_*, keep (tra_cash post_cash) se r2 
esttab DDD*, keep (tra_cash_bus post_cash_bus) se r2 


